package com.lionel.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 714936489@qq.com
 * @date 2018/5/16 22:13
 */
@Slf4j
public class SingletonExample2 {
    private static AtomicInteger count = new AtomicInteger(0);
    private SingletonExample2() {
        log.info("Singleton 私有的构造方法被实例化{}次：",count.incrementAndGet());
    }
    private static SingletonExample2 instance = new SingletonExample2();
    public static SingletonExample2 getInstance(){
        return instance;
    }

    public static void main1(String[] args) {
        SingletonExample2 instance1 = SingletonExample2.getInstance();
        SingletonExample2 instance2 = SingletonExample2.getInstance();
        log.info("object is the same:{}",instance1==instance2);
    }
}
@Slf4j
class SingletonTester{
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0;i<20;i++){
            executorService.execute(()->{
                log.info("ThreadName:{},Object:{}",Thread.currentThread().getName(),
                        SingletonExample2.getInstance());
            });
        }
        executorService.shutdown();
    }
}

